package org.swingDemo.UI.panel;

import net.miginfocom.swing.MigLayout;
import org.swingDemo.entity.PageInfo;

import javax.swing.*;

public class PagePanel extends JPanel {

    private JButton firstButton;
    private JButton previousButton;
    private JButton nextButton;
    private JButton lastButton;

    private PageInfo pageInfo;

    public PagePanel() {
        initPanel();
    }

    private void initPanel() {
        firstButton = new JButton("首页");
        firstButton.addActionListener(e -> gotoFirst());
        previousButton = new JButton("上一页");
        previousButton.addActionListener(e -> gotoPrevious());
        nextButton = new JButton("下一页");
        nextButton.addActionListener(e -> gotoNext());
        lastButton = new JButton("末页");
        lastButton.addActionListener(e -> gotoLast());

        this.setLayout(new MigLayout("fill", "push[][][][]push"));
        this.add(firstButton);
        this.add(previousButton);
        this.add(nextButton);
        this.add(lastButton);
    }

    private void gotoFirst() {
        pageInfo.gotoFirst();
        firePropertyChange("page_changed", 0, 1);
    }

    private void gotoPrevious() {
        pageInfo.gotoPrevious();
        firePropertyChange("page_changed", 0, 1);
    }

    private void gotoNext() {
        pageInfo.gotoNext();
        firePropertyChange("page_changed", 0, 1);
    }

    private void gotoLast() {
        pageInfo.gotoLast();
        firePropertyChange("page_changed", 0, 1);
    }

    public void setPageInfo(PageInfo pageInfo) {
        this.pageInfo = pageInfo;
    }

    public PageInfo getPageInfo() {
        return pageInfo;
    }
}
